package com.wzq.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzq.dao.pojo.*;
import com.wzq.service.IDisableTimeService;
import com.wzq.util.DateUtil;
import com.wzq.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/disabletime")
public class AdminDisabletimeController extends GetSome {


    @RequestMapping("/list")
    public R list(@RequestBody PageBean pageBean) {
        System.out.println(pageBean);
        String query = pageBean.getQuery().trim();
        Page<DisableTime> page = new Page<>(pageBean.getPageNum(), pageBean.getPageSize());
        Page<DisableTime> pageResult = iDisableTimeService.page(page, new QueryWrapper<DisableTime>().in(StringUtil.isNotEmpty(query), "p_id", searchByPname(query)));
        //        添加用户名称和项目名称
        List<DisableTime> list = new ArrayList<>();
        for (DisableTime disableTime : pageResult.getRecords()) {
            disableTime.setP_name(getP_name(disableTime.getP_id()));
            if (disableTime.getFormat() == 1) disableTime.setF_name("年月天时");
            else if (disableTime.getFormat() == 2) disableTime.setF_name("年月天");
            else disableTime.setF_name("年月");

            list.add(disableTime);


        }

        Map<String, Object> map = new HashMap<>();
        map.put("DisableTimeList", list);
        map.put("total", pageResult.getTotal());
        return R.ok(map);
    }

    // 禁用时间
    @PostMapping("/insert")
    public Result hour(@RequestBody DisableTime disableTime) {
//        try {
        if (disableTime.getFormat() == 1) {
//年月天时插入    20:00-21:30     按半小时插入
            String[] timelist = disableTime.getTimelist();
            String year = timelist[0].split(" ")[0];        // 年份
            String[] number = DateUtil.getNumber(timelist[0].split(" ")[1] + "-" + timelist[1].split(" ")[1]);       //半小时的切分  [ "20:00" , "20:30" ...]
            for (int i = 0; i < number.length - 1; i++) {       //  组合
                disableTime.setDisable_time(year + " " + number[i] + "-" + number[i + 1]);
                iDisableTimeService.save(disableTime);
            }
        } else if (disableTime.getFormat() == 2) {
            for (String n : disableTime.getTimelist()) {   //天
                disableTime.setDisable_time(n);
                iDisableTimeService.save(disableTime);
            }
        } else {
            String[] s = disableTime.getTimelist();      //年月  按月插入 YYYY-MM ~ YYYY-MM
            String date = s[0];
            while (s.length > 1 && !date.equals(s[1])) {                 // 开始月份    等于  结束月份
                disableTime.setDisable_time(date);
                iDisableTimeService.save(disableTime);
                if (Integer.parseInt(date.substring("2002-".length(), "2002-".length() + 2)) < 12)
                    date = addmonth(date);
                else {
                    date = Integer.parseInt(date.substring(0, 4)) + 1 + "-" + "01";
                }
            }
            disableTime.setDisable_time(date);
            iDisableTimeService.save(disableTime);
        }


//        } catch (Exception e){
////            return new Result(Code.SAVE_ERR,false,"系统繁忙，插入失败");
//            System.out.printf("123");
//        }
        return new Result(Code.SAVE_OK, true);
    }


    @GetMapping("/{id}")
    public R findById(@PathVariable(value = "id") Integer id) {
        DisableTime disabletime = iDisableTimeService.getById(id);
        //        获得用户名称和项目名称
        disabletime.setP_name(getP_name(disabletime.getP_id()));
        Map<String, Object> map = new HashMap<>();
        map.put("disabletime", disabletime);
        return R.ok(map);
    }


//    @RequestMapping("/save")
//    public R save(@RequestBody DisableTime disableTime){
//        if(disableTime.getId()==null || disableTime.getId()==-1){
//            iDisableTimeService.save(disableTime);
//        }else{
//            iDisableTimeService.saveOrUpdate(disableTime);
//        }
//        return R.ok();
//    }


    @GetMapping("/delete/{id}")
    public R delete(@PathVariable(value = "id") Integer id) {
        iDisableTimeService.removeById(id);
        return R.ok();
    }

    @RequestMapping("/listAll")
    public R listAll() {
        Map<String, Object> map = new HashMap<>();
        map.put("DisableTimeList", iDisableTimeService.list());
        return R.ok(map);

    }
}

